Shader "Converted/Sky05"
{
    Properties
    {
        _MainTex ("iChannel0", 2D) = "white" {}
        _NoiseTex ("_NoiseTex", 2D) = "white" {}
        
        _Ctrl("_Ctrl", Range(0, 20)) = 10
        _Clip("_Clip", Range(0, 1)) = 0
        _Scale("_Scale", Range(0, 3)) = 1
        _Mouse ("Mouse", Vector) = (0.5, 0.5, 0.5, 0.5)
        
        // 天空颜色属性
        [HDR]_SkyColorTop ("Sky Color Top", Color) = (0.0063, 0.2176, 0.8015, 1)
        [HDR]_SkyColorBottom ("Sky Color Bottom", Color) = (0.0063, 0.0063, 0.8015, 1)
        
        // 云属性
        _CloudCoverage ("Cloud Coverage", Range(0, 1)) = 0.64
        _CloudDetailCoverage ("Cloud Detail Coverage", Range(0, 1)) = 0.16
        _CloudSpeed ("Cloud Speed", Range(0, 10)) = 1.6
        _CloudDetailSpeed ("Cloud Detail Speed", Range(0, 20)) = 4.8
        _CloudAmbient ("Cloud Ambient", Range(0, 0.1)) = 0.01
        
        // 光线属性
        _MarchDistance ("March Distance", Range(0, 1)) = 0.35
        _Steps ("Raymarch Steps", Range(1, 30)) = 10
        _SunIntensity ("Sun Intensity", Range(0, 0.01)) = 0.002
        _SunFalloff ("Sun Falloff", Range(1, 5)) = 1.7
        _SunSmoothness ("Sun Smoothness", Range(0, 1)) = 0.4
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _NoiseTex;
            float4 _Mouse;
            float _Ctrl;
            float _Clip;
            float _Scale;
            
            // 天空颜色变量
            float4 _SkyColorTop;
            float4 _SkyColorBottom;
            
            // 云变量
            float _CloudCoverage;
            float _CloudDetailCoverage;
            float _CloudSpeed;
            float _CloudDetailSpeed;
            float _CloudAmbient;
            
            // 光线变量
            float _MarchDistance;
            float _Steps;
            float _SunIntensity;
            float _SunFalloff;
            float _SunSmoothness;
   
            #define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
            #define SAT(x) clamp(x, 0., 1.)
 
            // Global access to uv data
            static v2f vertex_output;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            float noise123(in float2 uv)
            {   
                return tex2D(_NoiseTex, uv).r * 1.5;
            }
          
            static const int OCTAVES = 6;
            float fbm123(in float2 uv)
            {
                float value = 0.;
                float amplitude = 0.5;
                for (int i = 0; i < OCTAVES; i++)
                {
                    value += noise123(uv) * amplitude;
                    amplitude *= 0.5;
                    uv *= 2.;
                }
                return value;
            }
 
            float clouds(float2 uv, float t)
            {   
                float c1 = fbm123(uv * _Ctrl + t);
                c1 = max(c1 - _Clip, 0) * _Scale;
                return c1;
            }

            float4 frag (v2f __vertex_output) : SV_Target
            {
                vertex_output = __vertex_output;
                float4 fragColor = 0;
                float2 uv = vertex_output.uv;
                float2 m = _Mouse.xy;
                float t = glsl_mod(_Time.y + 600., 7200.) * 0.03;
                float steps = _Steps;
                float stepsInv = 1. / steps;
                float2 sunDir = normalize(m - uv) * _MarchDistance * stepsInv;
                float2 marchUv = uv;
                
                float cloudColor = 1.;
                float cloudShape = clouds(uv, t);
                
                for (float i = 0.; i < _MarchDistance; i += _MarchDistance * stepsInv)
                {
                    marchUv += sunDir * i;
                    float c = clouds(marchUv, t);
                    cloudColor *= clamp(1. - c, 0., 1.);
                }
                
                cloudColor += _CloudAmbient;
                cloudColor = exp(-cloudColor) * (1. - exp(-cloudColor * 2.)) * 2.;
                cloudColor *= cloudShape;
                
                // 使用可调节的天空颜色
                float3 skyCol = lerp(_SkyColorBottom.rgb, _SkyColorTop.rgb, uv.y);

                float3 col = skyCol + cloudShape;
                col = lerp(cloudColor* 25., col, 1. - cloudShape);
                
                // 使用可调节的太阳参数
                float sun = _SunIntensity / pow(length(uv - m), _SunFalloff);
                col += (1. - smoothstep(0., _SunSmoothness, cloudShape)) * sun;
                
                fragColor = float4(sqrt(col), 1.);
                return fragColor;
            }
            ENDCG
        }
    }
}
